<?php
    /**
    * Get witness form
    * @author: trungdt
    * @since: Feb 8, 2011
    * @version: 1.0
    * 
    * Change logs
    * Feb 8, 2011 - trungdt - first create
    */

    if (!defined("EXPONENT")) exit("");
    $demand = null;
    $loc=null;
    $loc->mod="demandmodule";
    $id="";
    $witness=null;
    if (isset($_GET['id']))
    {
        $id=intval($_GET['id']);
        $demand=$db->selectObject("demands","id = {$id}");
        if (!isset($demand->id))
        {
            $demand=null;
            $demand->id=-1;
        }
    }
    else
    {
        $demand->id=-1;
    }

    if (isset($_GET['witness_id']))
    {
        $wid=intval($_GET['witness_id']);
        $witness=$db->selectObject("witness","id = {$wid}");    
    }


    if (exponent_users_isLoggedIn() ){
        if (($witness!=null) 
        && (!(exponent_permissions_check("manage",$loc) 
        || exponent_permissions_check("approve_demand",$loc))))
        {
            echo SITE_404_HTML;
        }
        else
        {
            $form = demands::witness_form($witness);
            //$form->location($loc);
            $form->meta("action","witness_save");
            // because we dont set $loc, we must set module name
            $form->meta("module","demandmodule");
            $form->meta("ajax_action",1);
            $form->meta("demand_id",$demand->id);
            // this div is for ajax result
            $form->registerBefore("submit",null,null,new htmlcontrol("<div id='submit_result' class='submit_result'></div>"));
            $template = new template("demandmodule","_witness_edit",$loc);
            $template->assign("demand_id",$demand->id);
            if ($witness!=null)
                $template->assign("is_edit",1);
            else
                $template->assign("is_edit",0);
            $template->assign("form_html",$form->toHTML());
            $template->output();   
        }
    }
    else
    {
        $tlink=exponent_core_makeLink(array(
            'module'=>'loginmodule',
            'action'=>'loginredirect'));
        header("Location: {$tlink}");
    }
?>
